package com.magicsoft.share.ui.article;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.constant.AppDavikActivityMgr;
import com.magicsoft.share.presenter.article.ArticleSettingPresenter;
import com.magicsoft.share.ui.my.AllShareActivity;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.article.IArticleSettingView;

import butterknife.BindView;
import butterknife.OnClick;

import static com.magicsoft.share.constant.Constant.REQUEST_ARTICLE_IS_COMMENT;
import static com.magicsoft.share.constant.Constant.REQUEST_ARTICLE_IS_OPEN;
import static com.magicsoft.share.constant.Constant.REQUEST_ARTICLE_TYPE;

public class ArticleSettingActivity extends BaseActivity<IArticleSettingView, ArticleSettingPresenter> implements IArticleSettingView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.layout_sort)
    RelativeLayout layoutSort;
    @BindView(R.id.layout_is_open)
    RelativeLayout layoutIsOpen;
    @BindView(R.id.layout_is_comment)
    RelativeLayout layoutIsComment;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_isopen)
    TextView tvIsopen;
    @BindView(R.id.tv_iscomment)
    TextView tvIscomment;
    private String titleStr;
    private String contentStr;
    private String coverStr;
    private String coverRiginalStr;
    private Integer isAllowComment = 1;
    private Integer isOpen = 1;
    private Integer articleType = 12;
    private Integer special;
    private Integer status = 1;
    private int articleId;
   private int  IsOpen=1;
   private int Is_allow_comment=1;
    @Override
    public ArticleSettingPresenter initPresenter() {
        return new ArticleSettingPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_article_setting;
    }

    @Override
    public void initData() {
        IsOpen=getIntent().getIntExtra("is_open",-1);
        Is_allow_comment=getIntent().getIntExtra("is_allow_comment",-1);

        titleStr = getIntent().getStringExtra("titleStr");
        contentStr = getIntent().getStringExtra("contentStr");
        coverStr = getIntent().getStringExtra("coverStr");
        coverRiginalStr = getIntent().getStringExtra("coverRiginalStr");


         articleId = getIntent().getIntExtra("articleId", -1);
         Log.e("DOODOD",isAllowComment+"");

        if(IsOpen==0){
            tvIsopen.setText("密码");
        }else if(IsOpen==1){
            tvIsopen.setText("公开");
        }

        if(Is_allow_comment==0){
          tvIscomment.setText("不允许");
        }if(Is_allow_comment==1){
            tvIscomment.setText("公开");
        }
    }

    @Override
    public void initView() {
        tvTitle.setText("文章设置");
        tvRight.setText("完成");

    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.ibtn_left, R.id.tv_right, R.id.layout_sort, R.id.layout_is_open, R.id.layout_is_comment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.tv_right:
                if (articleType == null || articleType == 0) {
                    ToastUtils.showToast(this, "请选择文章类型");
                } else {
                    if(IsOpen==-1){
                        Is_allow_comment=1;
                        IsOpen=1;}
                        mPresenter.articleEdit(articleId, titleStr, coverStr, coverRiginalStr, contentStr, Is_allow_comment, IsOpen, articleType, special, status);

                }

                break;
            case R.id.layout_sort:
                startActivityForResult(new Intent(this, SortActivity.class), REQUEST_ARTICLE_TYPE);
                break;
            case R.id.layout_is_open:
                startActivityForResult(new Intent(this, OpenActivity.class), REQUEST_ARTICLE_IS_OPEN);
                break;
            case R.id.layout_is_comment:
                startActivityForResult(new Intent(this, IsCommentActivity.class), REQUEST_ARTICLE_IS_COMMENT);
                break;
        }
    }

    @Override
    public void articleEditSuccess() {
        ToastUtils.showToast(this, "文章发布成功");
        AppDavikActivityMgr.getScreenManager().finishActivity(AddArticleActivity.class);
        AppDavikActivityMgr.getScreenManager().finishActivity(ArticlePreviewActivity.class);
        Intent intent = new Intent(this, AllShareActivity.class);
        intent.putExtra("position", 0);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {

                case REQUEST_ARTICLE_TYPE:
                    articleType = data.getIntExtra("articleType", 12);
                    tvType.setText(data.getStringExtra("articleTypeStr"));
                    break;
                case REQUEST_ARTICLE_IS_OPEN:
                    String openStr = data.getStringExtra("openStr");
                    IsOpen = data.getIntExtra("isopen", 1);

                    tvIsopen.setText(openStr);
                    break;
                case REQUEST_ARTICLE_IS_COMMENT:
                    String commentStr = data.getStringExtra("commentStr");
                    Is_allow_comment = data.getIntExtra("iscomment", 1);
                    tvIscomment.setText(commentStr);
                    Log.e("iscomment",isAllowComment+""+commentStr);
                    break;
            }
        }
    }


}
